package com.qf;

import com.qf.pojo.User;
import com.qf.pojo.UserExt;
import com.qf.service.OrdersService;
import com.qf.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author lixu
 */
public class SpringServletJunit {


    /**
     * Junit --> Service --> Dao
     */
    @Test
    public void testSpring() {

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-context.xml");

        UserService userService = (UserService) ac.getBean("userService");

        User user = userService.findUser();

        System.out.println(user);

    }

    /**
     *  UserExt 对象中属性
     */
    @Test
    public void testSpringUserExt() {

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-context.xml");
        //使用对象类型获取对象  前提 ： 此类型只能在Spring配置文件中出现一次
        UserExt userExt = ac.getBean(UserExt.class);

        System.out.println(userExt);

    }
    /**
     *  UserExt 对象中属性
     */
    @Test
    public void testSpringAutowire() {

        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        //使用对象类型获取对象  前提 ： 此类型只能在Spring配置文件中出现一次
        UserService bean = ac.getBean(UserService.class);
        User user = bean.findUser();
        System.out.println(user);

    }

    /**
     *   singleton : 单例
     *   scope="prototype"    | 多例
     */
    @Test
    public void testSpringSingleton() {

        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        //使用对象类型获取对象  前提 ： 此类型只能在Spring配置文件中出现一次
        UserService bean1 = ac.getBean(UserService.class);

        UserService bean2 = ac.getBean(UserService.class);

        System.out.println("===" + bean1);
        System.out.println("===" + bean2);

        /**
         * ===com.qf.service.UserServiceImpl@289d1c02
         * ===com.qf.service.UserServiceImpl@289d1c02
         */

    }
    /**
     * 生命周期
     */
    @Test
    public void testSpringLifeCycle() {

        //Spring容器   SpringIOC容器   IOC：控制反转
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

        OrdersService ordersService = ac.getBean(OrdersService.class);
        //关闭Spring容器
        ac.close();

    }
}
